#include "PlayerSelectionMenu.h"
#include "MenuManager.h"
#include "UserManager.h"
#include "ServiceProvider.h"
#include "GameSettings.h"

PlayerSelectionMenu::PlayerSelectionMenu(MenuManager* const i_menuManager)
	: Menu(i_menuManager)
	, m_numberOfUsersSelected(-1)
	, m_userCounter(0)
{
}

void PlayerSelectionMenu::Draw() const
{
	std::cout << "Welcome Users!" << std::endl << std::endl << std::endl;
	int i = GetSP()->GetSettings().GetValue("I_MAX_USERS");
	std::cout << "This game can be played from 0 to " << i << "." << std::endl;
	std::cout << "Please, enter the number of players: ";
	std::cout << std::endl;
	
	if (m_numberOfUsersSelected > 0)
	{
		std::cout << "Player " << m_userCounter + 1 << ": Please, insert your name: ";
	}
}

bool PlayerSelectionMenu::Input(const std::string& i_input)
{
	bool goodInput = true;
	
	if (m_numberOfUsersSelected == -1)
	{	
		int res = Utilities::StringToInt(i_input);
		int maxUsers = GetSP()->GetSettings().GetValue("I_MAX_USERS");
		if (res < 0 || res > maxUsers)
			goodInput = false;
		else
		{
			m_numberOfUsersSelected = res;
		}
	}
	else
	{
		GetSP()->GetUserMgr().AddUser(i_input);
		++m_userCounter;
	}

	if (m_numberOfUsersSelected == 0)
	{
		GetMM()->SetMenu(GetMM()->GetMainMenu());
	}
	else if ((static_cast<int>(m_userCounter) == m_numberOfUsersSelected))
	{
		m_numberOfUsersSelected = -1;
		m_userCounter = 0;
		GetMM()->SetMenu(GetMM()->GetTeamSelectionMenu());
	}
		//change state
	return goodInput;
}



